package com.lighthouse.note.base.controller;

import com.lighthouse.note.base.service.IKnowledgeNoteService;
import com.lighthouse.note.base.domain.KnowledgeNote;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 小记Controller
 * 
 * @author ysz
 * @date 2024-02-03
 */
@RestController
@RequestMapping("/knowledge/note")
public class KnowledgeNoteController extends BaseController
{
    @Autowired
    private IKnowledgeNoteService knowledgeNoteService;

    /**
     * 查询小记列表
     */
    @PreAuthorize("@ss.hasPermi('knowledge:note:list')")
    @GetMapping("/list")
    public TableDataInfo list(KnowledgeNote knowledgeNote)
    {
        startPage();
        List<KnowledgeNote> list = knowledgeNoteService.selectKnowledgeNoteList(knowledgeNote);
        return getDataTable(list);
    }

    /**
     * 导出小记列表
     */
    @PreAuthorize("@ss.hasPermi('knowledge:note:export')")
    @Log(title = "小记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KnowledgeNote knowledgeNote)
    {
        List<KnowledgeNote> list = knowledgeNoteService.selectKnowledgeNoteList(knowledgeNote);
        ExcelUtil<KnowledgeNote> util = new ExcelUtil<KnowledgeNote>(KnowledgeNote.class);
        util.exportExcel(response, list, "小记数据");
    }

    /**
     * 获取小记详细信息
     */
    @PreAuthorize("@ss.hasPermi('knowledge:note:query')")
    @GetMapping(value = "/{noteId}")
    public AjaxResult getInfo(@PathVariable("noteId") Long noteId)
    {
        return success(knowledgeNoteService.selectKnowledgeNoteByNoteId(noteId));
    }

    /**
     * 新增小记
     */
    @PreAuthorize("@ss.hasPermi('knowledge:note:add')")
    @Log(title = "小记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KnowledgeNote knowledgeNote)
    {
        return toAjax(knowledgeNoteService.insertKnowledgeNote(knowledgeNote));
    }

    /**
     * 修改小记
     */
    @PreAuthorize("@ss.hasPermi('knowledge:note:edit')")
    @Log(title = "小记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KnowledgeNote knowledgeNote)
    {
        return toAjax(knowledgeNoteService.updateKnowledgeNote(knowledgeNote));
    }

    /**
     * 删除小记
     */
    @PreAuthorize("@ss.hasPermi('knowledge:note:remove')")
    @Log(title = "小记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noteIds}")
    public AjaxResult remove(@PathVariable Long[] noteIds)
    {
        return toAjax(knowledgeNoteService.deleteKnowledgeNoteByNoteIds(noteIds));
    }
}
